function 获取选项(payload, 属性名) {
    处理通用场景(payload);
    let items = []
    if (typeof (payload.对象列表) != "undefined") {
        for (var i = 0; i < payload.对象列表.length; i++) {
            items[i] = {}
            items[i].label = payload.对象列表[i][属性名]
            items[i].value = payload.对象列表[i].id
        }
    }
    payload.data.options = items
    return { ...payload }
}

function 获取选项_左侧部分(payload) {
    处理通用场景(payload);
    let items = []
    let children = []
    if (typeof (payload.对象列表) != "undefined") {
        for (var i = 0; i < payload.对象列表.length; i++) {
            items[i] = {}
            items[i].label = payload.对象列表[i].名称
            items[i].value = payload.对象列表[i].id
            children[i] = { "ref": payload.对象列表[i].id, "defer": true }
        }
    }
    payload.data.options = []
    payload.data.options[0] = {}
    payload.data.options[0].leftOptions = items
    payload.data.options[0].children = children
    return { ...payload }
}

function 批量查询转换(payload) {
    处理通用场景(payload);
    let items = payload.对象列表
    if (typeof (items) == "undefined") { items = [] }
    payload.data.items = items
    payload.data.total = payload.总量
    return { ...payload }
}

function 单个查询转换(payload) {
    处理通用场景(payload);
    let data = payload.对象
    if (typeof (data) == "undefined") { data = {} }
    payload.data = data
    return { ...payload }
}

function 当前用户信息查询转换(payload) {
    处理通用场景(payload);
    let data = payload.对象
    if (typeof (data) == "undefined") { data = {} }
    payload.data = {}
    payload.data.用户 = data
    return { ...payload }
}

function 标准返回结果处理(payload) {
    处理通用场景(payload);
    return { ...payload }
}

function 处理通用场景(payload) {
    payload.status = payload.返回码
    payload.msg = payload.消息
    if (payload.返回码 != 0) {
        if (typeof (payload.msg) == "undefined") {
            payload.msg = "系统异常"
        }
    }
    if (typeof (payload.data) == "undefined") {
        payload.data = {}
    }
}

function 清理为空数据(api) {
    遍历清理空数据(api.body.对象)
    return {
        ...api,
        data: {
            ...api.data
        }
    }
}

function 遍历清理空数据(对象) {
    for (let cs in 对象) {
        if (对象[cs] instanceof Object) {
            遍历清理空数据(对象[cs])
            if (Object.keys(对象[cs]).length == 0) {
                delete 对象[cs]
            }
        } else {
            if (对象[cs] == "" || 对象[cs] == null) {
                delete 对象[cs]
            }
        }
    }
}

function 文件上传转换(payload) {
    处理通用场景(payload);
    payload.data.value = payload.对象.id
    return { ...payload }
}

function 删除url中参数(url,name){
  var e = eval('/'+name+'=[^&]*&?/g');
  url = url.replace(e, '');
  if(url.substr(-1)=='?'||url.substr(-1)=='&'){
     url = url.substr(0, url.length - 1); 
  }  
  return url;
}

function 启动app(){
window.enableAMISDebug = true
    let amisLib = amisRequire('amis');
    let amis = amisRequire('amis/embed');
    const match = amisRequire('path-to-regexp').match;

    const history = History.createBrowserHistory();
    const app = {
        type: 'app',
        data: {"当前用户会话":当前用户会话},
        brandName: '海鲨生产管理系统',
        logo: '/images/logo.png',
        header:
        {
          "type": "grid",
          "align": "right",
          "columns": [
            {
              "xs":1,
              "body": [
            {
                "type": "dropdown-button",
                "label": "${当前用户会话.关联用户.名}",
                "trigger": "hover",
                "buttons": [{
                    "type": "button",
                    "label": "退出登录",
                    "actionType": "ajax",
                    "api": "delete:/s/当前用户/会话/单个",
                    "redirect": "/"
                }]
            }
              ]
            }
          ]
        }
        ,
        // footer: '<div class="p-2 text-center bg-light">底部区域</div>',
        // asideBefore: '<div class="p-2 text-center">菜单前面区域</div>',
        // asideAfter: '<div class="p-2 text-center">菜单后面区域</div>',
        api: '/s/获取目录层级'
    };

    function isCurrentUrl(to, ctx) {
        if (!to) {
            return false;
        }
        const pathname = history.location.pathname;
        const link = to;

        if (!~link.indexOf('http') && ~link.indexOf(':')) {
            let strict = ctx && ctx.strict;
            return match(link, {
            decode: decodeURIComponent,
            strict: typeof strict !== 'undefined' ? strict : true
            })(pathname);
        }

        return decodeURI(pathname) === link;
    }

    let amisInstance = amis.embed(
        '#root',
        app,
        {
            location: history.location
        },
        {
            // watchRouteChange: fn => {
            //   return history.listen(fn);
            // },
            updateLocation: (location, replace) => {
            if (location === 'goBack') {
                return history.goBack();
            } else if (
                (!/^https?\:\/\//.test(location) &&
                    location ===
                    history.location.pathname + history.location.search) ||
                location === history.location.href
            ) {
                // 目标地址和当前地址一样，不处理，免得重复刷新
                return;
            } else if (/^https?\:\/\//.test(location) || !history) {
                return (window.location.href = location);
            }

            history[replace ? 'replace' : 'push'](location);
            },
            jumpTo: (to, action) => {
            if (to === 'goBack') {
                return history.goBack();
            }

            if (isCurrentUrl(to)) {
                return;
            }

            if (action && action.actionType === 'url') {
                action.blank === false
                    ? (window.location.href = to)
                    : window.open(to, '_blank');
                return;
            } else if (action && action.blank) {
                window.open(to, '_blank');
                return;
            }

            if (/^https?:\/\//.test(to) || to === '/') {
                window.location.href = to;
            } else if (
                (!/^https?\:\/\//.test(to) &&
                    to === history.pathname + history.location.search) ||
                to === history.location.href
            ) {
                // do nothing
            }
            else {
                history.push(to);
            }
            },
            isCurrentUrl: isCurrentUrl,
            theme: 'cxd'
        }
    );

    history.listen(state => {
        amisInstance.updateProps({
            location: state.location || state
        });
    });

    启动app();
}